package lp.xian.production.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import lp.xian.production.domain.Product;


import lp.xian.production.domain.ProductInventory;
import lp.xian.production.mapper.ProductInventoryMapper;
import lp.xian.production.mapper.ProductMapper;
import lp.xian.production.vo.ReturnInfoVo;
import org.apache.poi.ss.formula.functions.Finance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lp.xian.production.mapper.ReturnInfoMapper;
import lp.xian.production.domain.ReturnInfo;
import lp.xian.production.service.IReturnInfoService;

/**
 * 退货单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-19
 */
@Service
public class ReturnInfoServiceImpl implements IReturnInfoService
{
    @Autowired
    private ReturnInfoMapper returnInfoMapper;
   /* @Autowired
    private FinanceMapper financeMapper;*/
    @Autowired
    private ProductInventoryMapper productInventoryMapper;
    @Autowired
    private ProductMapper productMapper;
    /**
     * 查询退货单
     *
     * @param returnId 退货单主键
     * @return 退货单
     */
    @Override
    public ReturnInfo selectReturnInfoByReturnId(Integer returnId)
    {
        return returnInfoMapper.selectReturnInfoByReturnId(returnId);
    }

    /**
     * 查询退货单列表
     *
     * @param returnInfo 退货单
     * @return 退货单
     */
    @Override
    public List<ReturnInfo> selectReturnInfoList(ReturnInfo returnInfo)
    {
        return returnInfoMapper.selectReturnInfoList(returnInfo);
    }

    /**
     * 新增退货单
     *
     * @param returnInfo 退货单
     * @return 结果
     */
    @Override
    public int insertReturnInfo(ReturnInfo returnInfo)
    {
        returnInfo.setCreateTime(DateUtils.getNowDate());
        return returnInfoMapper.insertReturnInfo(returnInfo);
    }

    /**
     * 修改退货单
     *
     * @param returnInfo 退货单
     * @return 结果
     */
    @Override
    public int updateReturnInfo(ReturnInfo returnInfo)
    {
        returnInfo.setUpdateTime(DateUtils.getNowDate());
        return returnInfoMapper.updateReturnInfo(returnInfo);
    }

    /**
     * 批量删除退货单
     *
     * @param returnIds 需要删除的退货单主键
     * @return 结果
     */
    @Override
    public int deleteReturnInfoByReturnIds(Integer[] returnIds)
    {
        return returnInfoMapper.deleteReturnInfoByReturnIds(returnIds);
    }

    /**
     * 删除退货单信息
     *
     * @param returnId 退货单主键
     * @return 结果
     */
    @Override
    public int deleteReturnInfoByReturnId(Integer returnId)
    {
        return returnInfoMapper.deleteReturnInfoByReturnId(returnId);
    }

    @Override
    public int approverReturnInfo(ReturnInfoVo returnInfovo) {
        ReturnInfo returnInfo=new ReturnInfo();
        returnInfo.setReturnId(returnInfovo.getReturnId());
        returnInfo.setAuditStatus(Integer.parseInt(returnInfovo.getAuditStatus()));
        returnInfo.setApproverOpinion(returnInfovo.getApproverOpinion());
        returnInfo.setApproverPerson(SecurityUtils.getUsername());
        returnInfo.setApproverTime(new Date());
        int num=returnInfoMapper.updateReturnInfo(returnInfo);
       /* if (num>0){

            ReturnInfo returnInfo1=returnInfoMapper.selectReturnInfoByReturnId(returnInfovo.getReturnId());
            Finance yu=financeMapper.selectFinanceBalance();//查余额
            Finance finance=new Finance();
            finance.setFinanceName("【客户退货】"+returnInfo1.getReturnName());
            double doubleValue=returnInfo1.getMarketPrice().doubleValue();
            double nums=returnInfo1.getClientNum();
            double money=doubleValue * nums;
            BigDecimal fmoney=new BigDecimal(money);
            finance.setFinanceBalance(yu.getFinanceBalance());
            finance.setFinanceMoney(fmoney);
            finance.setFinanceStatus(0);
            finance.setClientId(returnInfo1.getClientId());
            finance.setCreateTime(new Date());
            finance.setCreateBy("系统生成");
            int num2=financeMapper.insertFinance(finance);
            if (num2>0) {
                //(1)如果是损坏 就返工
                if (returnInfo1.getReturnContent().intValue() == 1) {
                    ProductInventory productInventory = new ProductInventory();
                    productInventory.setFinishedProductName(returnInfo1.getReturnName());
                    productInventory.setFinishedProductNum(returnInfo1.getClientNum());
                    productInventory.setFinishedProductUnit(returnInfo1.getMarketUnit());
                    productInventory.setFinishedStatus(2);
                    productInventory.setCreateTime(new Date());
                    productInventory.setCreateBy("系统生成");
                    return productInventoryMapper.insertProductInventory(productInventory);
                    //(2)如果是其他原因 就返回仓库
                } else {
                    Product product = new Product();
                    //判断这个成品名称是否存在 存在则直接添加数量
                    Product product1 = productMapper.selectProductByProductName(returnInfo1.getReturnName());
                    if (returnInfo1.getReturnName().equals(product1.getProductName())) {
                        int productId = product1.getProductId();
                        product.setProductId(productId);
                        int proNum = product1.getProductNum() + returnInfo1.getClientNum();
                        product.setProductNum(proNum);
                        product.setUpdateBy(SecurityUtils.getUsername());
                        product.setUpdateTime(new Date());
                        return productMapper.updateProduct(product);
                        //成品名称不存在 添加新的库存
                    } else {
                        product.setProductName(returnInfo1.getReturnName());
                        product.setProductUnit(returnInfo1.getMarketUnit());
                        product.setProductNum(returnInfo1.getClientNum());
                        product.setCreateBy(SecurityUtils.getUsername());
                        product.setCreateTime(new Date());
                        return productMapper.insertProduct(product);
                    }
                }
            }*/

        return 0;
    }
}
